<template>
  <div class="home-box">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button class="search-btn" slot="title" type="badge" size="small" round icon="search" to="/search">搜索
      </van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <van-tabs class=" channel-tabs" v-model="active" animated swipeable>
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <article-list :channelId="item.id" />
      </van-tab>
      <div slot="nav-right" class="placeholder">
      </div>
      <div slot="nav-right" class="hamburger-btn">
        <i class="toutiao toutiao-gengduo" @click="isEditChannelShow=true"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑 -->
    <van-popup class="edit-channel-popup" v-model="isEditChannelShow" position="bottom" :style="{ height: '100%' }"
      closeable close-icon-position="top-left">
      <channel-edit :channels="channels" :active='active' @sendData="getEditData" />
    </van-popup>
    <!-- /频道编辑 -->
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'HomeIndex',
  data () {
    return {
      active: 0,
      // 定义数据接收频道列表
      channels: [],
      isEditChannelShow: false // 这里我们先设置为 true 就能看到弹窗的页面了
    }
  },
  methods: {
    // 获取频道列表
    async loadChannels () {
      // try {
      //   const { data } = await getUserChannels()
      //   this.channels = data.data.channels
      //   console.log(this.channels)
      // } catch (err) {
      //   this.$toast('获取频道列表数据失败')
      // }
      try {
        let channles = []
        const localChannels = getItem('TOUTIAO_CHANNELS')
        if (this.user || !localChannels) {
          // 登录 或者 本地没有存储 获取后端数据
          const { data } = await getUserChannels()
          this.channels = data.data.channels
          return false
        } else {
          // 未登录并且本地没有数据
          channles = localChannels
        }
        // 将数据更新到组件中
        this.channels = channles
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    // 获取子组件传值
    getEditData (e) {
      console.log(e)
      this.active = e.index
      this.isEditChannelShow = e.close
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: { ArticleList, ChannelEdit },
  created () {
    this.loadChannels()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.home-box {
  padding-top: 180px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      height: 80px;
      .van-tab__text {
        font-size: 27px;
        color: #777777;
      }
    }
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
    .van-tab--active .van-tab__text {
      color: #333 !important;
    }
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background: rgba(50, 150, 250, 1);
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }

  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: '';
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
  .edit-channel-popup {
    padding-top: 100px;
    box-sizing: border-box;
  }
}
</style>
